<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class CommentController extends Controller
{
    public function index(Request $request){

        $data = DB::table('t_comment as c1') 
            -> LeftJoin('t_goods as g1', 'g1.gid', '=', 'c1.gid') 
            -> LeftJoin('users','users.id','=','c1.uid') 
            -> select('c1.*','g1.gname','users.name as uname') 
            -> where('gname','like','%'.$request -> keywords.'%') 
            -> distinct('c1.gid')
            -> paginate($request -> input('num',10));

        return view('admin.comment.index', [
                'title' => '评论管理', 
                'data' => $data,
                'request' => $request -> all()
                ]);
    }
    public function del(Request $request){
        $data = DB::table('t_comment')
            -> where('id','=',$request -> id) 
            -> delete();
            if($data){
                return $request -> id;
            }else{
                return false;
            }
    }
    public function reply(Request $request){
        $id = $request -> id;
        $reply = $request -> reply;
        $res = DB::table('t_comment') 
            ->where('id',$id)
            ->update(['reply' => $reply,'rtime' => date('Y-m-d H:i:s')]);
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
}
